//
//  HelloWorldLayer.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 6/17/13.
//  Copyright Nguyen Tan Loc 2013. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"

// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
#import "CircleSegment.h"
#import "PhysicSystem.h"
#import "LevelManager.h"

#pragma mark - HelloWorldLayer

// HelloWorldLayer implementation
@implementation HelloWorldLayer

// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	HelloWorldLayer *layer = [HelloWorldLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super's" return value
	if( (self=[super init]) ) {
		
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"circle.plist" textureFilename:@"circle.png"];
        
        
//		CircleSegment *c = [[CircleSegment alloc] initWithType:TYPE_COLOR_BLUE radius:20];
//        [c setPosition:ccp(200, 200)];
//        [self addChild:c];
        
//        b = [[Bullet alloc] initWithType:TYPE_NORMAL radius:40];
//        [b setPosition:ccp(300, 300)];
//        [self addChild:b];
        
        
        LevelManager *lm = [[LevelManager alloc] init];
        [lm loadLevel:LEVEL_TEMPLATE];
        
        self.touchEnabled = true;
        [self scheduleUpdate];
	}
	return self;
}

-(void) update:(ccTime)delta
{
    [[PhysicSystem sharedPhysicSystem] update];
}

- (void) ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    
}

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView: [touch view]];
    CGPoint prevLocation = [touch previousLocationInView: [touch view]];
    
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
    prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation];
    
    CGPoint diff = ccpSub(touchLocation, prevLocation);
    
//    [b setPosition:ccp(b.position.x + diff.x, b.position.y + diff.y)];
};

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
